package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.im.IMUtils;
import com.zw.yzk.kabu.internal.component.DaggerMessageCenterComponent;
import com.zw.yzk.kabu.internal.component.DaggerMyMessageListComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.GetUnreadMessageCountModule;
import com.zw.yzk.kabu.internal.module.MessageListModule;
import com.zw.yzk.kabu.internal.module.ReadMessageModule;
import com.zw.yzk.kabu.management.TempData;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.presenter.MessageCenterPresenter;
import com.zw.yzk.kabu.view.view.MessageCenterView;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import io.rong.imlib.model.Conversation;

/**
 * Created by zhanwei on 2017/5/14.
 */

public class MessageCenterActivity extends BaseActivity implements MessageCenterView {

    @Bind(R.id.message_count)
    TextView messageCount;
    @Bind(R.id.comment_count)
    TextView commentCount;
    @Bind(R.id.light_count)
    TextView lightCount;

    @Inject
    MessageCenterPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_center;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.message_center))
                .build();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUnreadCount();
    }

    @Override
    public void initComponent() {
        DaggerMessageCenterComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .getUnreadMessageCountModule(new GetUnreadMessageCountModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    public void setCommentCount(int count) {
        commentCount.setText(getResources().getString(R.string.unread_count, count));
        commentCount.setVisibility(count == 0 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void setLightCount(int count) {
        lightCount.setText(getResources().getString(R.string.unread_count, count));
        lightCount.setVisibility(count == 0 ? View.GONE : View.VISIBLE);
    }

    @OnClick(R.id.layout_im)
    public void startChartList() {
        IMUtils.startSubConversationList(this, Conversation.ConversationType.PRIVATE);
    }

    @OnClick(R.id.layout_comment)
    public void startCommentListActivity() {
        startActivity(new Intent(this, CommentListActivity.class));
    }

    @OnClick(R.id.layout_light)
    public void startLightListActivity() {
        startActivity(new Intent(this, LightListActivity.class));
    }

    /**
     * 获取未读评论和点赞条目
     */
    private void getUnreadCount() {
        presenter.getUnreadCount();
        int imCount = TempData.getInstance().getUnReaDiMessage();
        if(imCount > 0) {
            messageCount.setText(getResources().getString(R.string.unread_count, imCount));
        }
    }
}
